package cn.sample.moreitems.menu;

import cn.sample.moreitems.entity.TechnologyEntity;
import cn.sample.moreitems.init.ModBlocks;
import cn.sample.moreitems.init.ModMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.DataSlot;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.SlotItemHandler;

public class TechnologyMenu extends AbstractContainerMenu {

    private final ContainerLevelAccess access;
    private final TechnologyEntity be;
    private final DataSlot price = DataSlot.standalone();   // 需要同步的整数
    private final DataSlot price2 = DataSlot.standalone();   // 需要同步的整数
    public TechnologyMenu(int id, Inventory inv, ContainerLevelAccess a, TechnologyEntity e) {
        super(ModMenu.THROWN_ROCK.get(), id);
        this.access = a; this.be = e;
        /* 燃料 原料 产物 */
        addSlot(new SlotItemHandler(e.getInventory(), 0, 56, 53));
        addSlot(new SlotItemHandler(e.getInventory(), 1, 56, 17));
        addSlot(new SlotItemHandler(e.getInventory(), 2, 116, 35));
        /* 玩家背包 */
        for (int row = 0; row < 3; row++)
            for (int col = 0; col < 9; col++)
                addSlot(new Slot(inv, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
        for (int col = 0; col < 9; col++)
            addSlot(new Slot(inv, col, 8 + col * 18, 142));

        addDataSlot(price);
        addDataSlot(price2);
    }

    /* 服务端每 tick 或事件触发时更新 */
    public void setPrice(int p) { price.set(p); }
    public int getPrice() { return price.get(); }

    public void setPrice2(int p) { price2.set(p); }
    public int getPrice2() { return price2.get(); }

    public TechnologyMenu(int id) {
        super(ModMenu.THROWN_ROCK.get(), id);
        this.access = null; this.be = null;
    }

    @Override
    public boolean stillValid(Player p) { return stillValid(access, p, ModBlocks.TECHNOLOGY_BLOCKS.get()); }

    /* 快速搬运自己写 */
    @Override
    public ItemStack quickMoveStack(Player p, int idx) {
        ItemStack itemStack = ItemStack.EMPTY;

        Slot slot = this.slots.get(idx);

        if (slot.hasItem()) {
            ItemStack itemstack1 = slot.getItem();
            itemStack = itemstack1.copy();
            if (idx < be.getInventory().getSlots()) {
                if (!this.moveItemStackTo(itemstack1, be.getInventory().getSlots(), this.slots.size(), false)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.moveItemStackTo(itemstack1, 0, be.getInventory().getSlots(), false)) {
                return ItemStack.EMPTY;
            }

            if (itemstack1.isEmpty()) {
                slot.setByPlayer(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }
        return itemStack;


    }

    public TechnologyEntity getBe() {
        return be;
    }
}
